package com.invengo.module_check.ui.fragment.inventory.item;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.module_check.R;
import com.invengo.module_check.ui.fragment.inventory.InventoryViewModel;

import androidx.annotation.NonNull;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.interceptor.logging.LocalLogUtil;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.cache.ACache;
import me.goldze.mvvmhabit.utils.cache.AKey;

import static com.invengo.library_db.entity.module_check.BookState.LENG_OUT_IN_SHELF;
import static com.invengo.library_db.entity.module_check.BookState.ERROR_NO_LISTING;
import static com.invengo.library_db.entity.module_check.BookState.ERROR_OTHER_SHELF;
import static com.invengo.library_db.entity.module_check.BookState.IN_SHELF;
import static com.invengo.library_db.entity.module_check.BookState.LENG_OUT;
import static com.invengo.library_db.entity.module_check.BookState.NEED_IN_SHELF;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java8.util.Optional;

public class InventoryItemViewModel extends ItemViewModel<InventoryViewModel> {
    public SingleLiveEvent<BookBean> book = new SingleLiveEvent<>();
    public SingleLiveEvent<String> shelfName = new SingleLiveEvent<>();
    public SingleLiveEvent<Integer> tipColor = new SingleLiveEvent<>();
    public SingleLiveEvent<String> tipContent = new SingleLiveEvent<>();
    public SingleLiveEvent<Boolean> isForce = new SingleLiveEvent<>();
    public SingleLiveEvent<Boolean> isNeedInventory = new SingleLiveEvent<>();

    public SingleLiveEvent<Boolean> isFirst = new SingleLiveEvent<>();

    public InventoryItemViewModel(@NonNull InventoryViewModel viewModel, BookBean bean) {
        super(viewModel);
        updateState(bean);
    }

    public BookBean bookBean;

    public void updateState(BookBean bean) {
        book.setValue(bean);
        this.bookBean = bean;
        this.bookBean.setTip(bean.getTip());
        LocalLogUtil.temp2File(bookBean.getBookName() + "  " + bookBean.getBarCode() + "  " + bookBean.getTip() + "  外借状态： " + bookBean.getState());
        String firstBookBarCode = viewModel.getFirstBookBarCode();
        if (ACache.getInstance().getAsBoolean(AKey.FEATURE_MODULE)) {
            boolean first = !TextUtils.isEmpty(firstBookBarCode) && bean.getBarCode().equals(firstBookBarCode);
            isFirst.setValue(first);
            isForce.setValue(first ? false : bean.getForceSortCase());
        } else {
            isFirst.setValue(false);
            isForce.setValue(false);
        }
        shelfName.setValue(Optional.ofNullable(bean.getShelfName()).orElse(""));
        isNeedInventory.postValue(bean.getbLostBookFlag() == 1);
        switch (bean.getTip()) {
            case ERROR_OTHER_SHELF:
                tipColor.setValue(viewModel.getApplication().getColor(R.color.color_red));
                tipContent.setValue(bean.getTip() + "\n" + bean.getShelfName());
                viewModel.addError(bean);
                break;
            case ERROR_NO_LISTING:
                tipColor.setValue(viewModel.getApplication().getColor(R.color.color_red));
                tipContent.setValue(bean.getTip());
                viewModel.addError(bean);
                break;
            case LENG_OUT_IN_SHELF:
                tipColor.setValue(viewModel.getApplication().getColor(R.color.color_red));
                tipContent.setValue(bean.getTip() + "\n 请取出");
                break;
            case LENG_OUT:
                tipColor.setValue(viewModel.getApplication().getColor(R.color.color_orange));
                tipContent.setValue(bean.getTip() + (StringUtils.isEmpty(bean.getNeedBackDate()) ? "，请取出" : (" 应归日期：" + bean.getNeedBackDate())));
                viewModel.addLengOut(bean);
                break;
            case IN_SHELF:
                tipColor.setValue(viewModel.getApplication().getColor(R.color.color_green));
                tipContent.setValue(bean.getTip());
                viewModel.addInShelf(bean);
                break;
            case NEED_IN_SHELF:
                tipColor.setValue(viewModel.getApplication().getColor(R.color.color_blue));
                tipContent.setValue(bean.getTip());
                break;
            default:
                KLog.e(">>>  " + bean.toString());
                break;
        }
    }

    public int getPosition() {
        return viewModel.getPosition(this) + 1;
    }

    public BindingCommand removeItem = new BindingCommand(() -> {
        viewModel.onItemRemove(this);
    });
}
